package com.hxzy.shop.controller.commodity;

import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageInfo;
import com.hxzy.shop.dto.commodity.req.*;
import com.hxzy.shop.dto.commodity.resp.DrugResp;
import com.hxzy.shop.dto.commodity.req.AddDrugReq;
import com.hxzy.shop.dto.commodity.req.ClassificationDrugPageReq;
import com.hxzy.shop.dto.commodity.req.DrugPageReq;
import com.hxzy.shop.dto.commodity.resp.ImportDrugResp;
import com.hxzy.shop.dto.commodity.resp.MarketingDrugListResp;
import com.hxzy.shop.entity.Drug;
import com.hxzy.shop.entity.StoreImportRecord;
import com.hxzy.shop.feign.commodity.CommodityFeign;
import com.hxzy.shop.utils.FastJsonUtils;
import com.hxzy.shop.utils.esayExcel.EsayExcelUtil;
import com.hxzy.shop.vo.ApiException;
import com.hxzy.shop.vo.Result;
import com.hxzy.shop.vo.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashSet;
import java.util.List;

import javax.annotation.Resource;

/**
 * @author sifan
 * @PackagName:com.yimeng.shop.controller.commodity
 * @ClassName: CommodityController
 * @Description:
 * @date:2021/8/4 15:22
 */
@RestController
@Api(tags = "商品管理")
@RequestMapping("/drug")
public class DrugController {

    @Resource
    private CommodityFeign commodityFeign;

    /**
     * 药品分页
     * @param drugPageReq 药品分页请求
     * @return 返回分页集合
     */
    @ApiOperation("获得药品分页")
    @PostMapping("/getDrugPage")
    public Result<PageInfo<Drug>> getDrugPage(@ApiParam(required = true, name = "drugPageReq", value = "药品分页请求model")
                                              @RequestBody DrugPageReq drugPageReq) {
        Result<PageInfo<Drug>> drugPage = commodityFeign.getDrugPage(drugPageReq);
        if (drugPage.getCode() != ResultCode.SUCCESS.getCode()) {
            throw new ApiException(500, drugPage.getMsg());
        }
        return Result.success(drugPage.getData());
    }
    @ApiOperation("添加商品和修改")
    @PostMapping("/addDrug")
    public  Result<Boolean> arrDrug(@ApiParam(required = true,name = "addDrugReq",value = "添加商品请求model")
                                    @RequestBody AddDrugReq addDrugReq){
        Result<Boolean> booleanResult=commodityFeign.addDrug(addDrugReq);
        if(booleanResult.getCode()!=ResultCode.SUCCESS.getCode()){
            throw new ApiException(500,booleanResult.getMsg());
        }
        return Result.success(booleanResult.getData());
    }
    /**
     * 根据分类获得药品
     */
    @ApiOperation("根据分类获得药品")
    @PostMapping("/getDrugPageByClassification")
    public Result<PageInfo<Drug>> getDrugPageByClassification(@RequestBody ClassificationDrugPageReq req){
        return commodityFeign.getDrugPageByClassification(req);
    }

    @PostMapping("/marketing/list")
    @ApiOperation("获取药物分页")
    public Result<PageInfo<MarketingDrugListResp>> findPageDrug(@RequestBody DrugPageReq drugPageReq) {
        if (drugPageReq.getClassificationName().size() != 0) {
            drugPageReq.setClassifiedManagementId(Long.parseLong(drugPageReq.getClassificationName().get(0)));
            drugPageReq.setSonClassifiedManagementId(Long.parseLong(drugPageReq.getClassificationName().get(1)));
        }
        return commodityFeign.findPageDrug(drugPageReq);
    }
    /**
         * 删除药品
         *有点问题数据不完整
         * @param idS 药品id
         * @return 是否成功
         */
        @ApiOperation("删除药品")
        @PostMapping("/deleteDrug")
        public Result<Boolean> deleteDrug(@ApiParam(required = true, name = "ids", value = "删除的id数组")
                                          @RequestBody List<Long> idS) {
            Result<Boolean> booleanResult = commodityFeign.deleteDrug(idS);
            if (booleanResult.getCode()!=ResultCode.SUCCESS.getCode()){
                throw new ApiException(500, booleanResult.getMsg());
            }
            return Result.success(booleanResult.getData());
        }




     /**
     * 更新状态
     *
     * @param state 状态
     * @return 是否成功
     */
    @ApiOperation("更新状态")
    @PostMapping("/updateState")
    public Result<Boolean> updateState(@ApiParam(readOnly = true, name = "state", value = "状态")
                                       @RequestBody DrugUpdateReq state) {
        Result<Boolean> booleanResult = commodityFeign.updateState(state);
        if (booleanResult.getCode() != ResultCode.SUCCESS.getCode()) {
            throw new ApiException(500, booleanResult.getMsg());
        }
        return Result.success(booleanResult.getData());
    }
    /**
     * 获得药品详细
     *
     * @param req 药品id
     * @return 药品详细
     */
    @ApiOperation("获得药品详细")
    @PostMapping("/getOneDrug")
    public Result<DrugResp> getOneDrug(@RequestBody DrugInfoReq req) {
        Result<DrugResp> oneDrug = commodityFeign.getOneDrug(req);
        if (oneDrug.getCode() != ResultCode.SUCCESS.getCode()) {
            throw new ApiException(500, oneDrug.getMsg());
        }
        return Result.success(oneDrug.getData());
    }

    /**
     * 批量上下架
     */
    @ApiOperation("批量上下架")
    @PostMapping("/updateBatchState")
    public Result<Object> updateBatchState(@RequestBody EditClassifyDto dto) {
        Boolean aBoolean = commodityFeign.updateBatchState(dto);
        if (Boolean.TRUE.equals(aBoolean)) {
            return Result.success();
        }
        return Result.failed();
    }


    /**
     * 批量修改分类
     */
    @ApiOperation("批量修改分类")
    @PostMapping("/updateClassify")
    public Result<Object> updateClassify(@RequestBody EditClassifyDto dto) {
        Boolean aBoolean = commodityFeign.updateClassify(dto).getData();
        if (Boolean.TRUE.equals(aBoolean)) {
            return Result.success(aBoolean);
        }
        return Result.failed("添加分类失败");
    }


     /**
     * 批量修改药品信息
     */
    @ApiOperation("批量修改药品信息")
    @PostMapping("/batchEditDrugDetail")
    public Result<ImportDrugResp> batchEditDrugDetail(MultipartFile file){
        return commodityFeign.batchEditDrugDetail(file);
    }

//    @PostMapping("/getEditStoreImportRecord/{storeImportRecordId}")
//    @ApiOperation("获取修改药品错误的数据")
//    public void getEditStoreImportRecord(@PathVariable("storeImportRecordId") Long storeImportRecordId, HttpServletResponse response) {
//        Result<StoreImportRecord> record = commodityFeign.getStoreImportRecord(storeImportRecordId);
//        if(ObjectUtil.isEmpty(record.getData())){
//            throw new ApiException(500,"没有错误记录！");
//        }
//        StoreImportRecord data = record.getData();
//        String record1 = data.getRecord();
//        List<BatchEditDrugReq> batchEditDrugReqList = FastJsonUtils.json2list(record1, BatchEditDrugReq.class);
//        HashSet<String> hashSet = new HashSet<String>();
//        hashSet.add("stairClassifiedId");
//        hashSet.add("secondLevelClassifiedId");
//        hashSet.add("merchantId");
//        hashSet.add("drugId");
//        EsayExcelUtil.exportExcel(batchEditDrugReqList,hashSet,response,BatchEditDrugReq.class);
//    }
}
